package net.caixiaomi.info.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import net.caixiaomi.info.Lottery.ui.BallLotteryMainActivity;
import net.caixiaomi.info.WebViewActivity;
import net.caixiaomi.info.ui.details.ArticleDetailActivity;
import net.caixiaomi.info.ui.discovery.ActiveCenterActivity;
import net.caixiaomi.info.ui.discovery.FindActivity;
import net.caixiaomi.info.ui.discovery.LeaguesHomeActivity;
import net.caixiaomi.info.ui.discovery.OpenPrizeActivity;
import net.caixiaomi.info.ui.discovery.ProfessorClassActivity;
import net.caixiaomi.info.ui.football.PaymentActivity;
import net.caixiaomi.info.ui.football.PlayFootballActivity;
import net.caixiaomi.info.ui.football.detail.MatchDetailActivity;
import net.caixiaomi.info.ui.login.LoginActivity;
import net.caixiaomi.info.ui.login.RegistActivity;
import net.caixiaomi.info.ui.mine.RechargeActivity;

public class SchemeUtil {


    public static boolean parseIntentUrl(Context context, String url, String eventId) {
        boolean result = false;
        String type = "";
        String id = "";
        String subId = "";
        String title = "";
        String extparam = "";
        try {

            if (TextUtils.isEmpty(url))
                return result;
            if (!url.contains("cxmxc")) {
                startThirdPay(context, url);
                return result;
            }
            Uri parse = Uri.parse(url);
            type = parse.getQueryParameter("type");
            id = parse.getQueryParameter("id");
            subId = parse.getQueryParameter("subid");
            title = parse.getQueryParameter("title");
            extparam = parse.getQueryParameter("extparam");

            Intent intent = new Intent();
            if (!TextUtils.isEmpty(type)) {
                Integer intType = Integer.valueOf(type);
                if (intType == 1) {//
                    intent.setClass(context, WebViewActivity.class);
                    intent.putExtra("eventId", eventId);
                    intent.putExtra("url", url);
                } else if (intType == 3) {
                    if(TextUtils.equals(id,"1")){
                        intent.setClass(context, PlayFootballActivity.class);
                        intent.putExtra("playType", subId);
                    }else if(TextUtils.equals(id,"2")){
                        intent.setClass(context, BallLotteryMainActivity.class);
                        intent.putExtra("playType", subId);
                    }else{
                        ToastUtil.s("敬请期待");
                        return false;
                    }

                } else if (intType == 4) {
                    intent.setClass(context, MatchDetailActivity.class);
                    intent.putExtra("data", id);
                } else if (intType == 8) {
                    intent.setClass(context, ArticleDetailActivity.class);
                    intent.putExtra("data", id);
                } else if (intType == 5) {
                    intent.setClass(context, LoginActivity.class);
                } else if (intType == 6) {
                    intent.setClass(context, RegistActivity.class);
                }else if(intType==9){   //发现种类
                    int lId=Integer.parseInt(id);
                    switch (lId){
                        case 1: //开奖结果
                            intent.setClass(context, OpenPrizeActivity.class);
                            break;
                        case 2: //专家广场

                            break;
                        case 3: //彩票学堂
                            intent.setClass(context, ProfessorClassActivity.class);
                            break;
                        case 4: //活动中心
                            intent.setClass(context, ActiveCenterActivity.class);
                            break;
                        case 5: //资讯信息
//                            intent.setClass(context, FindActivity.class);
                            intent.setClass(context, WebViewActivity.class);
                            intent.putExtra("url", Constants.BASE_H5_URL + "faxian/news?from=app_find");
                            break;
                        case 6: //晒单公园

                            break;
                        case 7: //实时统计
                            intent.setClass(context, LeaguesHomeActivity.class);
                            break;
                        case 8: //更多

                            break;
                    }
                }
            }
            ComponentName component = intent.getComponent();
            if (component != null || intent.getData() != null) {
                context.startActivity(intent);
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static boolean parseInWebViewUrl(Context context, String url, @Nullable String param) {
        boolean result = false;
        String type = "";
        String id = "";
        String subId = "";
        String title = "";
        String extparam = "";
        String cxmxc = "";
        try {

            if (TextUtils.isEmpty(url))
                return result;

            Uri parse = Uri.parse(url);
            type = parse.getQueryParameter("type");
            id = parse.getQueryParameter("id");
            subId = parse.getQueryParameter("subid");
            title = parse.getQueryParameter("title");
            extparam = parse.getQueryParameter("extparam");
            cxmxc = parse.getQueryParameter("cxmxc");
            if (TextUtils.isEmpty(cxmxc)) {
                return result;
            }
            Intent intent = new Intent();
            if (!TextUtils.isEmpty(type)) {
                Integer intType = Integer.valueOf(type);
                if (intType == 1) {//
                    intent.setClass(context, WebViewActivity.class);
                    intent.putExtra("url", url);
                } else if (intType == 3) {
                    if(TextUtils.equals(id,"1")){
                        intent.setClass(context, PlayFootballActivity.class);
                        intent.putExtra("playType", subId);
                    }else if(TextUtils.equals(id,"2")){
                        intent.setClass(context, BallLotteryMainActivity.class);
                        intent.putExtra("playType", subId);
                    }else{
                        ToastUtil.s("敬请期待");
                        return false;
                    }
                } else if (intType == 4) {
                    intent.setClass(context, MatchDetailActivity.class);
                    intent.putExtra("data", id);
                } else if (intType == 8) {
                    intent.setClass(context, ArticleDetailActivity.class);
                    intent.putExtra("data", id);
                } else if (intType == 5) {
                    intent.setClass(context, LoginActivity.class);
                } else if (intType == 6) {
                    intent.setClass(context, RegistActivity.class);
                } else if (intType == 10) {
                    intent.setClass(context, PaymentActivity.class);
                    intent.putExtra("data", param);
                    intent.putExtra("action", true);
                } else if (intType == 11) {
                    intent.setClass(context, RechargeActivity.class);
                    intent.putExtra("data", param);
                }
            }

            ComponentName component = intent.getComponent();
            if (component != null || intent.getData() != null) {
                context.startActivity(intent);
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    private static void startThirdPay(Context context, String payUrl) {
        Uri uri = Uri.parse(payUrl);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }

}
